<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_container">
        <div class="avatar_box">
          <img src="../assets/logo.png" alt="" />
        </div>
      </div>
      <div class="login_form_container">
        <!-- ref引用视图层实例对象 -->
        <el-form
          ref="loginFormRef"
          label-width="0px"
          :model="loginForm"
          :rules="loginFormRules"
        >
          <el-form-item label="" prop="username">
            <el-input
              placeholder="请输入用户名"
              prefix-icon="iconfont icon-yonghu"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item label="" prop="password">
            <el-input
              placeholder="请输入密码"
              prefix-icon="iconfont icon-mimabeifen"
              type="password"
              v-model="loginForm.password"
            ></el-input>
          </el-form-item>
        </el-form>
        <div class="login_btn_container">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 登录信息表单
      loginForm: {
        username: "admin",
        password: "123456"
      },
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名！", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码!", trigger: "blur" },
          { min: 3, max: 16, message: "长度在 3 到 16 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    reset: function() {
      this.$refs.loginFormRef.resetFields();
    },
    login: function() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) {
          return;
        }
        // 发送axios请求 axios POST请求里面不是一个对象
        // const result =await this.$axios.post("login", this.loginForm);
        // 这里推荐使用对象的解构,data是原本对象里面的一个属性
        const { data: result } = await this.$axios.post(
          "login",
          this.loginForm
        );
        // console.log(result);
        // console.log(result.meta.status);
        var status = result.meta.status;
        if (status != 200) {
          // console.log('登录失败');
          this.$message.error("登录失败");
          return;
        }
        // 登录成功
        this.$message({
          message: "登录成功",
          type: "success"
        });
        // 本地存储sessionStorage
        window.sessionStorage.setItem('browserToken',result.data.token)
        this.$router.push('/home')
      });
    }
  }
};
</script>
<style lang="less" scoped>
.login_container {
  height: 100%;
  position: relative;
  .login_box {
    width: 450px;
    height: 300px;
    position: absolute;
    background-color: #fff;
    left: 50%;
    top: 50%;
    margin-left: -225px;
    margin-top: -150px;
    border-radius: 5px;
    .avatar_container {
      width: 100px;
      height: 100px;
      border-radius: 50%;
      background-color: #fff;
      padding: 5px;
      border: 1px solid #eee;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      .avatar_box {
        width: 100px;
        height: 100px;
        border-radius: 50%;
        box-shadow: 0 0 10px #ddd;
        background-color: #ddd;
        img {
          width: 100%;
          height: 100%;
          border-radius: 50%;
        }
      }
    }
    .login_form_container {
      position: absolute;
      bottom: 50px;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;
      .login_btn_container {
        // position: absolute;
        // right: 20px;
        display: flex;
        // justify-content: end;
        justify-content: flex-end;
      }
    }
  }
}
</style>
